package iss.java.list;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wenke on 2016/9/16.
 * // TODO: Modify this class to meet requirement B and C.
 */
 public class MyList extends Thread {
    // two guards. Do not call remove() on them!!
    private Node head;
    private Node tail;
    private int size;
    private int num;

    //create a new list
    public MyList() {
        num = 59;
        head = new Node().setData(0);
        tail = new Node().setData(0).setNext(null).setPrev(head);
        head.setNext(tail);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public Node getHead() {
        return head;
    }

    public Node getTail() {
        return tail;
    }

    public static Lock lock = new ReentrantLock();

    /**
     * Insert a node with <pre>data</pre> after <pre>_prev</pre>.
     *
     * @param _prev
     * @param data
     * @return The node just inserted.
     */
    public Node insert(Node _prev, int data) {
        lock.lock();
        try {
            Node node = new Node().setData(data).setNext(_prev.getNext()).setPrev(_prev);
            _prev.getNext().setPrev(node);
            _prev.setNext(node);
            ++size;
            return node;
        } finally {
            lock.unlock();
        }
    }

    /**
     * Remove the node <pre>target</pre>.
     *
     * @param target The node to remove.
     * @return the previous node of target.
     */
    public Node remove(Node target) {
        lock.lock();
        try {
            if (target == head || target == tail)
                throw new RuntimeException("DO NOT remove the head or tail node. They are guards.");
            // shortcut "target"
            Node prev = target.getPrev();
            Node next = target.getNext();
            prev.setNext(next);
            next.setPrev(prev);
            --size;
            return prev;
        } finally {
            lock.unlock();
        }
    }
}//end class MyList












